#include "stdafx.h"
#include "listctrl_wrapper.h"

listctrl_wrapper::listctrl_wrapper()
{

}

listctrl_wrapper::~listctrl_wrapper()
{

}

BEGIN_MESSAGE_MAP(listctrl_wrapper, CListCtrl)
	//{{AFX_MSG_MAP(CXListCtrl)
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL listctrl_wrapper::SetCheckbox(int nItem, int nSubItem, int nCheckedState)
{
	ASSERT(nItem >= 0);
	ASSERT(nItem < GetItemCount());
	if ((nItem < 0) || nItem >= GetItemCount())
		return FALSE;
	ASSERT(nSubItem >= 0);
	ASSERT(nSubItem < GetColumns());
	if ((nSubItem < 0) || nSubItem >= GetColumns())
		return FALSE;
	ASSERT(nCheckedState == 0 || nCheckedState == 1 || nCheckedState == -1);

	listctrl_data *pXLCD = (listctrl_data *) CListCtrl::GetItemData(nItem);
	if (!pXLCD)
	{
		return FALSE;
	}

	// update checkbox in subitem

	pXLCD[nSubItem].nCheckedState = nCheckedState;

	UpdateSubItem(nItem, nSubItem);


	return TRUE;
}

int listctrl_wrapper::GetCheckbox(int nItem, int nSubItem)
{
	ASSERT(nItem >= 0);
	ASSERT(nItem < GetItemCount());
	if ((nItem < 0) || nItem >= GetItemCount())
		return -1;
	ASSERT(nSubItem >= 0);
	ASSERT(nSubItem < GetColumns());
	if ((nSubItem < 0) || nSubItem >= GetColumns())
		return -1;

	listctrl_data *pXLCD = (listctrl_data *) CListCtrl::GetItemData(nItem);
	if (!pXLCD)
	{
		return -1;
	}

	return pXLCD[nSubItem].nCheckedState;
}

int listctrl_wrapper::CountCheckedItems(int nSubItem)
{
	ASSERT(nSubItem >= 0);
	ASSERT(nSubItem < GetColumns());
	if ((nSubItem < 0) || nSubItem >= GetColumns())
		return 0;

	int nCount = 0;

	for (int nItem = 0; nItem < GetItemCount(); nItem++)
	{
		listctrl_data *pXLCD = (listctrl_data *) CListCtrl::GetItemData(nItem);
		if (!pXLCD)
		{
			continue;
		}

		if (pXLCD[nSubItem].nCheckedState == 1)
			nCount++;
	}

	return nCount;
}

int listctrl_wrapper::GetColumns()
{
	return GetHeaderCtrl()->GetItemCount();
}

void listctrl_wrapper::UpdateSubItem(int nItem, int nSubItem)
{
	ASSERT(nItem >= 0);
	ASSERT(nItem < GetItemCount());
	if ((nItem < 0) || nItem >= GetItemCount())
		return;
	ASSERT(nSubItem >= 0);
	ASSERT(nSubItem < GetColumns());
	if ((nSubItem < 0) || nSubItem >= GetColumns())
		return;

	CRect rect;
	if (nSubItem == -1)
	{
		GetItemRect(nItem, &rect, LVIR_BOUNDS);
	}
	else
	{
		GetSubItemRect(nItem, nSubItem, LVIR_BOUNDS, rect);
	}

	rect.InflateRect(2, 2);

	InvalidateRect(&rect);
	UpdateWindow();
}

void listctrl_wrapper::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
	int a = 0;
	int b = 0;
	b = a + 3;
}

BOOL listctrl_wrapper::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	// TODO: Add your specialized code here and/or call the base class
	if ( message == WM_LISTCTRL_EVENT_LBTNDOWN )
	{
		CString str;
		int nItem = (int)wParam;
		CString strID = (LPCTSTR)lParam;
		//str.Format( _T("CListCtrlEx: Recieve Message itemid=%d, strID=%s"), nItem, strID);
		//MessageBox( str, _T("Recieve Message"), MB_OK);
	}

	return CListCtrl::OnWndMsg(message, wParam, lParam, pResult);
}
